package com.lzl.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzl.common_utils.JwtUtils;
import com.lzl.common_utils.R;
import com.lzl.eduorder.entity.Order;
import com.lzl.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/eduorder/order")
//@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    //生成订单的方法
    @PostMapping("createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId,
                       HttpServletRequest request){

        //创建订单，返回订单号
        String orderNo = orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderNo",orderNo);
    }

    //根据订单id查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("order",order);
    }

    /**
     * 被调用---根据课程id 和 用户id 查询订单表中 订单的状态
     * 查看课程是否已经被购买
     * @param courseId
     * @param memberId 用户id 为什么不用request请求得到，用户信息都放在请求头中
     * @return
     */
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable("courseId") String courseId,
                               @PathVariable("memberId") String memberId){

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_Id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);
        //目前只需要看表里有无这条记录，不需要查出来
        int count = orderService.count(wrapper);

        if(count>0){
            //表示已经支付
            return true;
        }else {
            return false;
        }
    }


}

